/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pmetihc;

import java.awt.event.KeyEvent;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

/**
 *
 * @author Mauricio
 */
public class ValidaPaso extends ValidadorEnteros {

	JProgressBar pb;
	JTextField tf;
	boolean usado;

	public ValidaPaso (JTextField tf, JProgressBar pb) {
		usado = false;
		this.pb = pb;
		this.tf = tf;
	}

	@Override
	public void keyTyped (KeyEvent e) {
		super.keyTyped (e);
		char caracter = e.getKeyChar ();

		System.out.println (tf.getText ().length ());
		if (caracter == KeyEvent.VK_BACK_SPACE) {
			System.out.println ("es bs");
		}
		if (tf.getText ().isEmpty ()) {
			System.out.println ("tf esta vacio");
		}
		if ((caracter < '0' || caracter > '9') && caracter != KeyEvent.VK_BACK_SPACE) {
			e.consume ();
		} else if (caracter == KeyEvent.VK_BACK_SPACE && tf.getText ().isEmpty ()) {
			if (usado) {
				pb.setValue (pb.getValue () - 1);
				usado = false;
			} else {
				e.consume ();
			}
		} else {
			if (tf.getText ().isEmpty () && (caracter != KeyEvent.VK_BACK_SPACE && caracter != KeyEvent.VK_DELETE) && !usado) {
				pb.setValue (pb.getValue () + 1);
				usado = true;
			}
		}
	}
}
